package com.lagou.homework.servlet;

import com.lagou.homework.bean.Student;
import com.lagou.homework.service.StudentManageService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 实现获取所有学生信息的Servlet
 */
@WebServlet(name = "ShowAllStudentsServlet", urlPatterns = "/showAllStudents")
public class ShowAllStudentsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.创建StudentManageService对象，调用方法获取Student数组
        StudentManageService studentManageService = new StudentManageService();
        Student[] allStudents = studentManageService.getAllStudentsService();

        // 2.将获取到的数组传入session，实现浏览器跳转，跳转到主页面
        request.getSession().setAttribute("allStudents", allStudents);
        response.sendRedirect("main.jsp");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
